import { UnknownObjProps } from '@/interface/public';
import { TabsPaneContext } from 'element-plus';

export interface DrawerDataProps {
  openDrawer: (row: any, activeName?: string) => void;
  drawerData: {
    visible: boolean;
    isClose: boolean;
    activeName: string;
    row: UnknownObjProps;
  };
}
/**
 * @name 弹框
 */
export function useDrawerData(refresh: Function, activeName: string, row: any = { id: '' }) {
  let drawerData = reactive<DrawerDataProps['drawerData']>({
    visible: false,
    isClose: false,
    activeName: activeName,
    row: row
  });

  const changeDrawerDataStatus = (flag: boolean) => {
    drawerData.visible = flag;
  };

  // 切换 tabs
  const switchoverTabs = (pane: TabsPaneContext) => {
    drawerData.activeName = pane.paneName as string;
    refresh();
  };

  const openDrawer = (row: any, activeName?: string) => {
    if (activeName) {
      drawerData.activeName = activeName;
    }
    drawerData.row = row;
    Object.assign(drawerData, {
      visible: true,
      isClose: false
    });

    setTimeout(() => {
      refresh();
      drawerData.isClose = true;
    }, 300);
  };
  return {
    drawerData,
    switchoverTabs,
    openDrawer,
    changeDrawerDataStatus
  };
}
